package controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.sql.SQLException;
import java.sql.Timestamp;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import manager.LeaveDetailManager;
import manager.LeaveManager;

import data.dao.LeaveDAOException;
import data.dao.daoImplementation.NotFoundException;
import data.dto.LeaveDTO;
import data.dto.LeaveDetailCommentDTO;
import data.dto.UserDTO;
import data.searchdto.LeaveSearchDTO;

/**
 * Servlet implementation class ManageDetailLeaveServlet
 */

@WebServlet("/ManageDetailLeaveServlet")
public class ManageDetailLeaveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ManageDetailLeaveServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (LeaveDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param leaves
	 * @param ins
	 * @throws NotFoundException
	 * @throws SQLException
	 * @throws LeaveDAOException
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try {
			doProcess(request, response);
		} catch (LeaveDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws LeaveDAOException,
			SQLException, NotFoundException, ServletException, IOException {
		String page = "";
		UserDTO user = util.CurrentUser.getCurrentUser(request.getSession());

		// check user
		if (user == null) {
			page = util.Constants.homePage;
		} else if (util.CurrentUser.isAdmin(user)) {
			page = util.Constants.homePage;
		} else {

			// String btn = request.getParameter("btnApply")== null :
			String type = request.getParameter("btnApply") == null ? "reject"
					: "approve";
			String id = request.getParameter("id") == null ? "0" : request
					.getParameter("id").trim();
			String comment = request.getParameter("comment");

			int iid = Integer.parseInt(id);
			LeaveManager lm = new LeaveManager();
			if (type.equalsIgnoreCase("approve")) {
				lm.updateLeaveDetail(
						util.Constants.LEAVE_APPLICATION_STATUS_APPROVED, iid);
			} else if (type.equalsIgnoreCase("reject")) {
				lm.updateLeaveDetail(
						util.Constants.LEAVE_APPLICATION_STATUS_REJECTED, iid);
				LeaveDetailManager LDM = new LeaveDetailManager();
				LeaveDetailCommentDTO leaveDetailComment = new LeaveDetailCommentDTO();
				leaveDetailComment.setLeaveID(iid);
				leaveDetailComment.setComment(comment);
				LDM.insertLeaveDetailComment(leaveDetailComment);
			}

			// ArrayList<LeaveDTO> data = lm.findAllLeave();
			// request.setAttribute("Leaves", data);
			page = "/ViewLeaveServlet";
		}
		
		RequestDispatcher rd = request.getRequestDispatcher(page);
		rd.forward(request, response);

	}
}
